package com.qbccn.blog.config.exception;

import com.alibaba.fastjson.JSONObject;
import com.qbccn.blog.dto.ResponseCode;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author linke
 * @date 2019-12-28 下午 13:40
 * @description 权限不足处理
 */
@Component
public class CustomAccessDeniedHandler implements AccessDeniedHandler {

    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException accessDeniedException) throws IOException, ServletException {
        response.setContentType("application/json;charset=UTF-8");
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code", ResponseCode.NO_AUTHORITY);
        jsonObject.put("message", "没有权限访问");
        jsonObject.put("data", accessDeniedException.getMessage());
        response.getWriter().write(jsonObject.toString());
    }
}
